package com.cy.aircompressor.main.ui.lifecycle.fragment.provider;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.cy.aircompressor.databinding.ItemComponentLayoutBinding;
import com.cy.aircompressor.entity.component.Component;
import com.cy.aircompressor.entity.sensor.Sensor;
import com.drug.lib.view.recycptr.ItemViewBinder;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent;
import com.net.model.base.BaseBindViewHolder;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class SensorProvider extends ItemViewBinder<Sensor, SensorProvider.ViewHolder> {
    Context mContext;

    public SensorProvider(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    protected SensorProvider.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(ItemComponentLayoutBinding.inflate(inflater, parent, false));

    }

    @Override
    protected void onBindViewHolder(@NonNull SensorProvider.ViewHolder holder, @NonNull Sensor entity) {
        holder.mBinding.serial.setText(String.valueOf(holder.getAdapterPosition()));
        holder.mBinding.name.setText(entity.getName());
        holder.mBinding.sn.setText(entity.getSn());
        RxTextView.textChangeEvents(holder.mBinding.afterSn)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TextViewTextChangeEvent>() {
                    @Override
                    public void accept(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
                        String input = holder.mBinding.afterSn.getText().toString().trim();
                        entity.setNewSn(input);
                    }
                });
    }


    public class ViewHolder extends BaseBindViewHolder<ItemComponentLayoutBinding> {

        protected ViewHolder(ItemComponentLayoutBinding itemView) {
            super(itemView);
        }
    }


}
